package com.test.controller.pagination;

import java.util.HashMap;
import java.util.Map;

import com.test.dao.FiddlerTemplateRepository;
import com.test.entities.FiddlerTemplate;
import com.test.entities.Persons;
import org.springframework.stereotype.Component;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.test.dao.PersonsRepository;



@Component
class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        if (SpringUtil.applicationContext == null) {

            SpringUtil.applicationContext = applicationContext;

        }
    }

    public static ApplicationContext getApplicationContext() {

        return applicationContext;

    }

    public static Object getBean(String name) {

        return getApplicationContext().getBean(name);

    }

    public static <T> T getBean(Class<T> clazz) {

        return getApplicationContext().getBean(clazz);

    }

    public static <T> T getBean(String name, Class<T> clazz) {

        return getApplicationContext().getBean(name, clazz);

    }
}


interface Types {

    public Page<FiddlerTemplate> query();

    public Integer getCount();

    public Integer getPageNumber();

    public Long getTotal();

    public Object getContent();
}

class BasePaginationInfo {

    public Pageable pageable;

    public FiddlerTemplateRepository instance = SpringUtil.getBean(FiddlerTemplateRepository.class);

    public String finalTestResult, responseData;

    public BasePaginationInfo(String finalTestResult, String responseData, Pageable pageable) {

        this.pageable = pageable;

        this.finalTestResult = finalTestResult;

        this.responseData = responseData;
    }
}

class AllType extends BasePaginationInfo implements Types {


    public AllType(String finalTestResult, String responseData, Pageable pageable) { //String sexName, String emailName,

        super(finalTestResult, responseData, pageable);

    }

    public Page<FiddlerTemplate> query() {

        return this.instance.findAll(

                this.pageable

        );
    }

    public Integer getCount() {
        return this.query().getSize();
    }

    public Integer getPageNumber() {

        return this.query().getNumber();

    }

    public Long getTotal() {
        return this.query().getTotalElements();
    }

    public Object getContent() {
        return this.query().getContent();
    }
}

class FinalTestResultResponseDataType extends BasePaginationInfo implements Types {

    public FinalTestResultResponseDataType(String finalTestResult, String responseData, Pageable pageable) {

        super(finalTestResult, responseData, pageable);

    }

    public Page<FiddlerTemplate> query() {

        return this.instance.findByFinalTestResultAndResponseDataContains(

                this.finalTestResult,

                this.responseData,

                this.pageable
        );
    }

    public Integer getCount() {
        return this.query().getSize();
    }

    public Integer getPageNumber() {

        return this.query().getNumber();

    }

    public Long getTotal() {
        return this.query().getTotalElements();
    }

    public Object getContent() {
        return this.query().getContent();
    }


}

class SexType extends BasePaginationInfo implements Types {

    public SexType(String finalTestResult, String responseData, Pageable pageable) { //String sexName, String emailName,

        super(finalTestResult, responseData, pageable);
    }

    public Page<FiddlerTemplate> query() {

        return this.instance.findByFinalTestResult(

                this.finalTestResult,

                this.pageable
        );
    }

    public Integer getCount() {
        return this.query().getSize();
    }

    public Integer getPageNumber() {

        return this.query().getNumber();

    }

    public Long getTotal() {
        return this.query().getTotalElements();
    }

    public Object getContent() {
        return this.query().getContent();
    }
}


public class PaginationFormatting {

    private PaginationMultiTypeValuesHelper multiValue = new PaginationMultiTypeValuesHelper();

    private Map<String, PaginationMultiTypeValuesHelper> results = new HashMap<>();

    public Map<String, PaginationMultiTypeValuesHelper> filterQuery(String finalTestResult, String responseData, Pageable pageable) {

        Types typeInstance;

        if (finalTestResult.length() == 0 && responseData.length() == 0) {

            typeInstance = new AllType(finalTestResult, responseData, pageable);

        } else if (finalTestResult.length() > 0 && responseData.length() > 0) {

            typeInstance = new FinalTestResultResponseDataType(finalTestResult, responseData, pageable);

        } else {
            typeInstance = new SexType(finalTestResult, responseData, pageable);
        }

        this.multiValue.setCount(typeInstance.getCount());

        this.multiValue.setPage(typeInstance.getPageNumber() + 1);

        this.multiValue.setResults(typeInstance.getContent());

        this.multiValue.setTotal(typeInstance.getTotal());

        this.results.put("data", this.multiValue);

        return results;
    }

}